<?php
/**
 * 数组基类
 * @author sunhaoqiang(shqdev@163.com)
 * @time: 2019-12-30 10:05
*/
namespace script;

use \IteratorIterator;
use \ArrayIterator;

class Conf extends ArrayIterator implements \ArrayAccess
{
	protected $data = [];

	public function __construct(Array $data = [])
	{
		$this->data = array_merge($this->data, $data);
		// 让对象可以迭代
		parent::__construct($this->data);
	}

	public function offsetExists($key)
	{
		return isset($this->data[$key]);
	}

	public function offsetSet($key, $value)
	{
		$this->data[$key] = $value;
	}

	public function offsetGet($key)
	{
		return $this->data[$key];
	}

	public function offsetUnset($key)
	{
		unset($this->data[$key]);
	}

	/**
	 * 数组返回
	 *
	 * @return void
	 * @author sunhaoqiang(shqdev@163.com)
	 * @time 2019-12-30 10:18
	 */
	public function array()
	{
		return $this->data;
	}

	/**
	 * 数组切片
	 *
	 * @param [type] $start
	 * @param [type] $end
	 * @return void
	 * @author sunhaoqiang(shqdev@163.com)
	 * @time 2019-12-30 10:22
	 */
	public function slice($start, $end)
	{
		return array_slice($this->data, 0, count($this->data)-($start+$end));
	}

	public function __get($key)
	{
		return $this->data[$key] ?? null;
	}

	public function __set($key, $value)
	{
		$this->data[$key] = $value;
	}

	public function __invoke(Array $data = [])
	{
		$this->data = array_merge($this->data, $data);
		return $this;
	}
}
